package com.bytz.modules.cms.domain.impl.order;

import cn.hutool.core.util.BooleanUtil;
import com.bytz.common.exception.BytzBootException;
import com.bytz.modules.cms.domain.MallOrderProcessor;
import com.bytz.modules.cms.domain.model.order.MallOrderProcessContext;
import com.bytz.modules.cms.product.constant.ProductConstants;
import com.bytz.modules.cms.product.model.ProductModel;

import java.util.Collection;

/**
 * 订单销售处理器 判断订单产品是否下架
 *
 * @author fengwenhao
 */
public class MallOrderProductCheckProcessor extends MallOrderAbstractProcessor implements MallOrderProcessor {


    public MallOrderProductCheckProcessor(int sequence) {
        super(sequence);
    }

    @Override
    public void doProcess(MallOrderProcessContext context) {
        if (BooleanUtil.isTrue(context.getSubmit())) {
            Collection<ProductModel> products = context.getProducts();
            boolean hasDisableProduct = products.stream()
                    .anyMatch(product -> !ProductConstants.ProductStatus.ENABLE.equals(product.getStatus()));
            if (hasDisableProduct) {
                throw new BytzBootException("存在已下架产品，无法提交订单");
            }
        }
    }
}